var net = require("net");
function _(str){
	buf=new Buffer(str.length+1);
		buf.write(str);
		buf.writeUInt8(0xFF,buf.length-1);
		return buf;
}
function startSocket(port,io){
	var server = net.createServer(function(socket){
		//socket.pipe(socket);
		io.emit('newMsg',{name: "鹳狸猿",content:"传感器已加入"});
		console.log("Sensor Join!");
		socket.on("end",function(){
			console.log("Sensor Quit");
			io.emit('newMsg',{name: "鹳狸猿",content:"传感器已退出"});
		});
		
		socket.write(_("hello"));
		socket.on("data",function(data){
			console.log("客户端说："+data.toString());
			console.log(data[0]);
			console.log(data);
			console.log(data[1]);
			console.log(data.toJSON());
			io.emit('newMsg',{name: "鹳狸猿",content:"从传感器传来的数据："+data.toString()});
			socket.write(_(data.toString()));
		});
		
	});
	server.listen(port,function(){
		console.log("Server Start!");
	});
}

exports.startSocket=startSocket;